/*
@(#)PageResult   2023-04-17
 
Copyright (c) 2011-2023 杭州湖畔网络技术有限公司 
保留所有权利 
本软件为杭州湖畔网络技术有限公司所有及包含机密信息，须遵守其相关许可证条款进行使用。
Copyright (c) 2011-2023 HUPUN Network Technology CO.,LTD.
All rights reserved.
This software is the confidential and proprietary information of HUPUN
Network Technology CO.,LTD("Confidential Information").  You shall not
disclose such Confidential Information and shall use it only in
accordance with the terms of the license agreement you entered into with HUPUN.
Website：http://www.hupun.com
*/
package org.obp.data.result;

import com.baomidou.mybatisplus.core.metadata.IPage;
import io.swagger.annotations.ApiModel;
import io.swagger.annotations.ApiModelProperty;
import lombok.AllArgsConstructor;
import lombok.Data;
import org.obp.data.param.PageParam;
import org.springframework.util.CollectionUtils;

import java.io.Serializable;
import java.util.Collections;
import java.util.List;

/**
 * @author: Jackey 2023/4/17
 */
@Data
@ApiModel("分页数据")
@AllArgsConstructor
public final class PageResult<T> implements Serializable {

    private static final long serialVersionUID = 487597100693244247L;

    @ApiModelProperty("数据对象")
    private List<T> data;

    @ApiModelProperty("总页数")
    private Integer totalPage = 0;

    @ApiModelProperty("总条数")
    private Integer totalCount = 0;

    @ApiModelProperty("分页参数")
    private PageParam pageParam;

    public PageResult() {
        this.data = Collections.emptyList();
    }

    public PageResult(IPage<T> result) {
        this.totalPage = (int) result.getPages();
        this.totalCount = (int) result.getTotal();
        this.pageParam = new PageParam(result.getCurrent(), result.getSize());
        this.data = Collections.emptyList();
        if (!CollectionUtils.isEmpty(result.getRecords())) {
            this.data = result.getRecords();
        }
    }

    public static <T> PageResult<T> of(final List<T> data, final Integer totalPage, final Integer totalCount, final PageParam pageParam) {
        return new PageResult<>(data, totalPage, totalCount, pageParam);
    }

    public static <T> PageResult<T> ofEmpty() {
        return new PageResult<>(Collections.emptyList(), 0, 0, new PageParam(0, 0));
    }

    public static <T> PageResult<T> ofEmpty(int pageNum, int pageSize) {
        return new PageResult<>(Collections.emptyList(), 0, 0, new PageParam(pageNum, pageSize));
    }

    public static <T> PageResult<T> ofEmpty(final PageParam pageParam) {
        return new PageResult<>(Collections.emptyList(), 0, 0, new PageParam(pageParam.getPageNum(), pageParam.getPageSize()));
    }

}